package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.R;
import com.example.administrator.zhongyichou.adapter.DiseaseDetailsGridAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.help.ImageLoader;
import com.example.administrator.zhongyichou.help.StringImageToList;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.LookRaiseProtocol;
import com.example.administrator.zhongyichou.view.CircleImageView;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.view.MyGridView;
import com.example.administrator.zhongyichou.view.PromiseDialog;
import com.fynn.fluidlayout.FluidLayout;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.preview.ImagePreviewActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/22.
 * 描述：
 */

public class DiseaseDetailsActivity extends BaseActivity<LookRaiseProtocol> implements AdapterView.OnItemClickListener {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.txt_help_details)
    TextView txtHelpDetails;
    @BindView(R.id.txt_show_all)
    TextView txtShowAll;
    @BindView(R.id.fl_content)
    FluidLayout flContent;
    @BindView(R.id.fl_content1)
    FluidLayout flContent1;
    @BindView(R.id.fl_content2)
    FluidLayout flContent2;
    @BindView(R.id.gridview)
    MyGridView gridview;
    @BindView(R.id.txt_time)
    TextView txtTime;
    @BindView(R.id.txt_time0)
    TextView txtTime0;
    @BindView(R.id.txt_time1)
    TextView txtTime1;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.txt_aims_money)
    TextView txtAimsMoney;
    @BindView(R.id.txt_got_money)
    TextView txtGotMoney;
    @BindView(R.id.txt_raise_num)
    TextView txtRaiseNum;
    @BindView(R.id.img_avatar)
    CircleImageView imgAvatar;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_help)
    Button txtHelp;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_disease)
    TextView txtDisease;
    @BindView(R.id.txt_payee)
    TextView txtPayee;
    private DiseaseDetailsGridAdapter adapter;
    private ArrayList<String> list;
    private String infoID;
    private LookRaiseProtocol mGsonBaseProtocal;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
    }

    //添加证明
    private void addProof() {
        if (!TextUtils.isEmpty(mGsonBaseProtocal.getData().getSupply_info().getReal_name())) {
            flContent.addView(createSuccessView("身份证明已审核"));
        }
        flContent1.addView(createSuccessView("诊断证明已审核"));
        flContent1.addView(createSuccessView("诊断医院：" + mGsonBaseProtocal.getData().getSupply_info().getHospital_add()));
        flContent2.addView(createSuccessView("身份证明已审核"));
        flContent2.addView(createSuccessView("关系证明已审核 "));
    }

    //成功的View
    private View createSuccessView(String string) {
        View view = LayoutInflater.from(this).inflate(R.layout.disease_details_type, null);
        TextView text = (TextView) view.findViewById(R.id.txt_content);
        text.setText(string);
        return view;
    }

    //失败的View
    private View createErrorView(String string) {
        View view = LayoutInflater.from(this).inflate(R.layout.disease_details_type, null);
        ImageView img = (ImageView) view.findViewById(R.id.img);
        TextView text = (TextView) view.findViewById(R.id.txt_content);
//        text.setTextColor(Color.parseColor(""));
        text.setText(string);
        return view;
    }

    @Override
    protected void responseData(LookRaiseProtocol mGsonBaseProtocal) {
        this.mGsonBaseProtocal = mGsonBaseProtocal;
        ImageLoader.centerCropUrl(this, StringImageToList.toImage(mGsonBaseProtocal.getData().getHead_img()), imgAvatar);
        if (mGsonBaseProtocal.getData().getUser_tel().length() == 11) {
            txtPhone.setText(mGsonBaseProtocal.getData().getUser_tel().substring(0, 3) + "****" + mGsonBaseProtocal.getData().getUser_tel().substring(mGsonBaseProtocal.getData().getUser_tel().length() - 4, mGsonBaseProtocal.getData().getUser_tel().length()));
        } else {
            txtPhone.setText(mGsonBaseProtocal.getData().getUser_tel());
        }
        if (mGsonBaseProtocal.getData().getAudit_status().equals("5")) {//已结束
            txtTime.setVisibility(View.GONE);
            txtTime1.setVisibility(View.GONE);
            txtTime0.setText("已结束");
            txtHelp.setEnabled(false);
            txtHelp.setText("项目已结束");
            txtHelp.setBackgroundColor(getResources().getColor(R.color.white));
            txtHelp.setTextColor(getResources().getColor(R.color.appColor));
        } else {
            txtTime.setText(mGsonBaseProtocal.getData().getLast_day() + "");
        }
        txtAimsMoney.setText(mGsonBaseProtocal.getData().getAims_money() + "");
        txtGotMoney.setText(mGsonBaseProtocal.getData().getGot_money() + "");
        txtRaiseNum.setText(mGsonBaseProtocal.getData().getRaise_num());
        txtTitle.setText(mGsonBaseProtocal.getData().getInfo_title());
        txtHelpDetails.setText(mGsonBaseProtocal.getData().getInfo_content());
        txtName.setText("患者:   " + mGsonBaseProtocal.getData().getSupply_info().getReal_name());
        txtDisease.setText("所患疾病:   " + mGsonBaseProtocal.getData().getSupply_info().getDisease_name());
        switch (mGsonBaseProtocal.getData().getSupply_info().getReceive_type()) {
            case "0":
                txtPayee.setText("收款人：" + mGsonBaseProtocal.getData().getSupply_info().getReceive_name() + "(患者本人)");
                break;
            case "1":
                txtPayee.setText("收款人：" + mGsonBaseProtocal.getData().getSupply_info().getReceive_name() + "(直系亲属)");
                break;
            case "2":
                txtPayee.setText("收款人：" + mGsonBaseProtocal.getData().getSupply_info().getReceive_name() + "(患者配偶)");
                break;
        }
        list = (ArrayList<String>) StringImageToList.toList(mGsonBaseProtocal.getData().getInfo_img());
        adapter = new DiseaseDetailsGridAdapter(this, list);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(this);
        addProof();
    }

    @Override
    protected HttpLoader initHttpLoader() {
        infoID = getIntent().getStringExtra("info_id");
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(LookRaiseProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("info_id", infoID);
        requestHttpLoader.setParams(NetConfig.LOOK_RAISE, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.disease_details_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    @OnClick({R.id.txt_show_all, R.id.txt_help, R.id.ll_promise})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_show_all://显示全部
                Intent intentAll = new Intent(this, ContentActivity.class);
                intentAll.putExtra("content", mGsonBaseProtocal.getData().getInfo_content());
                startActivity(intentAll);
                break;
            case R.id.txt_help:
                Intent intent = new Intent(this, PayHelpActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_promise:
                PromiseDialog dialog = new PromiseDialog(this);
                dialog.show();
                break;
        }
    }

    private void initHead() {
        headerview.setTitle("筹款详情");
        headerview.setRightText("分享");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                    case HeaderView.RIGHT:
                        break;
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        List<ImageInfo> imageInfo = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageInfo info = new ImageInfo();
            info.bigImageUrl = list.get(i);
            info.thumbnailUrl = list.get(i);
            //拿到view的位置
            View imageView;
            imageView = gridview.getChildAt(position);
            info.imageViewWidth = imageView.getWidth();
            info.imageViewHeight = imageView.getHeight();
            int[] points = new int[2];
            imageView.getLocationInWindow(points);
            info.imageViewX = points[0];
            info.imageViewY = points[1] - getStatusBarHeight();
            imageInfo.add(info);
        }
        // 启动预览.
        Intent intent = new Intent(this, ImagePreviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ImagePreviewActivity.IMAGE_INFO, (Serializable) imageInfo);
        bundle.putInt(ImagePreviewActivity.CURRENT_ITEM, position);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }
}
